package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.vo.RoleVo;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    UserService userService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页", notes = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto", value = "用户DTO对象", required = true, dataType = "roleDto"),
            @ApiImplicitParam(paramType = "path", name = "pageNum", value = "页码", example = "1", dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "pageSize", value = "每页条数", example = "10", dataType = "Integer")
    })
    public ResponseResult<PageResponse<UserVo>> findUserVoPageResponse(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<UserVo> pageResponse = userService.findUserVoPageResponse(userDto, pageNum, pageSize);
        return ResponseResult.success(pageResponse);
    }

    @PutMapping
    @ApiOperation(value = "用户添加", notes = "用户添加")
    public ResponseResult<UserVo> addUser(@RequestBody UserDto userDto) {
        UserVo user = userService.addUser(userDto);
        return ResponseResult.success(user);
    }

    @PatchMapping
    @ApiOperation(value = "用户修改", notes = "用户修改")
    public ResponseResult updateUser(@RequestBody UserDto userDto) {
        Boolean flag = userService.updateUser(userDto);
        return ResponseResult.success(flag);
    }

    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation(value = "用户启用-禁用", notes = "用户启用-禁用")
    public ResponseResult isEnable(@PathVariable("id") Long id, @PathVariable("status") String status) {
        Boolean flag = userService.isEnable(id, status);
        return ResponseResult.success(flag);
    }
    @DeleteMapping("/remove/{userIds}")
    @ApiOperation(value = "用户删除", notes = "用户删除")
    public ResponseResult remove(@PathVariable("userIds") String userIds) {
        Boolean flag = userService.remove(userIds);
        return ResponseResult.success(flag);
    }

    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation(value = "重置密码", notes = "重置密码")
    public ResponseResult resetPasswords(@PathVariable("userId") Long userId) {
        Boolean flag = userService.resetPasswords(userId);
        return ResponseResult.success(flag);
    }

    @PostMapping("/list")
    @ApiOperation(value = "用户列表", notes = "用户列表")
    public ResponseResult<List<UserVo>> userList(@RequestBody UserDto userDto) {
        UserVo userVos = userService.userList(userDto);
        return ResponseResult.success(userVos);
    }
}
